package com.tlw.leave.service.impl;

import com.tlw.leave.dao.InformationDao;
import com.tlw.leave.dao.InformationRecordDao;
import com.tlw.leave.entity.Information;
import com.tlw.leave.entity.InformationRecord;
import com.tlw.leave.entity.Vo.Conditions;
import com.tlw.leave.entity.Vo.LeaveTime;
import com.tlw.leave.entity.Vo.PageVo;
import com.tlw.leave.service.InformationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Sort;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

@Service
public class InformationServiceImpl implements InformationService {
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    @Autowired
    private InformationDao informationDao;

    @Autowired
    private InformationRecordDao informationRecordDao;

    @Transactional
    @Override
    public Information addInformation(Information information) {

        System.out.println(information);
        information.setCreateTime(new Date());
        information.setTerminatetime(new Date());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        double num1 = information.getStartDate().getTime();
        double num2 = information.getEndDate().getTime();
        int day =(int) ((num2 - num1)/86400000);
        double hour =(((num2 - num1)%86400000)/3600000);
        information.setLeaveTime(day + "天" + (int)hour + "小时");

//        information.setCategory();

        Information save = informationDao.save(information);
        InformationRecord informationRecord=new InformationRecord();
        BeanUtils.copyProperties(information,informationRecord);
        informationRecord.setUpdeteName(information.getName());
        informationRecord.setInformationId(save.getId());
        informationRecordDao.save(informationRecord);
        return save;
    }

    @Transactional
    @Override
    public void deleteInformation(Integer id) {
        informationDao.deleteById(id);
    }

    @Transactional
    @Override
    public Information updateInformation(Information information) {

        InformationRecord informationRecord=new InformationRecord();

        BeanUtils.copyProperties(information,informationRecord);
        informationRecord.setUpdeteName(information.getName());
        informationRecord.setInformationId(information.getId());
        informationRecordDao.save(informationRecord);
//        LOGGER.info(new Date()+"用户执行了"+Thread.currentThread().getStackTrace()[1].getMethodName());  //查看当前方法名
        return informationDao.saveAndFlush(information);
    }

    @Override
    public List<Information> finadAll() {
        return informationDao.findAll();
    }

    @Override
    public Optional<Information> getInformationById(Integer id) {
        return informationDao.findById(id);
    }

    @Override
    public List<Information> findInformationName(String name) {
        return informationDao.findInformationName(name);
    }

    @Override
    public List<Information> getPageList(Integer page, Integer total) {
        return informationDao.getPageList(page,total);
    }

    @Override
    public Page<Information> getPageableList(PageVo pageVo) {

        if (pageVo.getPageNum() == null) {
            pageVo.setPageNum(1);
        }

        Sort sort=Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of(pageVo.getPageNum() - 1,pageVo.getSize(),sort);
        Page<Information> all = informationDao.findAll(pageable);
          for(Information data:all){
//               data.setStartDate((Date)df.format(data.getStartDate()));
//              df.format();
//              df.parse(df.format(data.getStartDate()));
//              System.out.println(df.format(data.getStartDate()));
              Date date=new Date();
              try {
                  date=df.parse(df.format(data.getStartDate()));
                  data.setStartDate(date);
              } catch (ParseException e) {
                  e.printStackTrace();
              }
//              System.out.println(df.format(data.getStartDate()));
          }
        return all;
    }

    @Override
    public List<Information> getTimeScope(LeaveTime leaveTime) {
        return informationDao.getPageListTime(leaveTime);
    }

    @Override
    public List<Information> getconditionsListInformation(Conditions conditions) {
        Specification specification=new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (conditions.getName()!=null){
                    predicates.add(criteriaBuilder.equal(root.get("name"), conditions.getName()));
                }
                if (conditions.getCategory()!=null){
                    predicates.add(criteriaBuilder.equal(root.get("category"), conditions.getCategory()));
                }
                criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
                return criteriaQuery.getRestriction();
            }
        };

        return informationDao.findAll(specification);
    }

//    @Override
//    public List<Information> getconditionsListInformation(Conditions conditions) {
//
//        Specification specification=new Specification() {
//            @Override
//            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
//                List<Predicate> predicates = new ArrayList<>();
//                List<Predicate> temp = new ArrayList<>();
//                if (conditions.getName() != null) {
//                    temp.add(criteriaBuilder.equal(root.get("name"),conditions.getName()));
//                }
//                if (conditions.getCategory()!=null){
//                    temp.add(criteriaBuilder.equal(root.get("category"),conditions.getCategory()));
//                }
//                predicates.add(criteriaBuilder.or(temp.toArray(new Predicate[temp.size()])));
//                return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
//            }
//        };
//        return informationDao.find(specification);
//    }

}
